package com.leesche.zcsp.node.app.v1.controller.discount;
import com.leesche.zcsp.node.app.v1.controller.admin.AppAdminController;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDeviceDiscount;
import com.leesche.zcsp.node.web.model.PubDiscount;
import com.leesche.zcsp.node.web.service.PubDeviceDiscountService;
import com.leesche.zcsp.node.web.service.PubDiscountService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDiscountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceDiscountCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDiscountCondition;
import com.leesche.zcsp.node.web.service.impl.data.writ.UpdateBatchDiscountCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/11/15.
 * 设备套餐模块
 */
@Controller
@RequestMapping("/app/v1/discount")
public class AppDiscountController extends AppAdminController {
//public class AppDiscountController {

    private final static Logger LOGGER = LoggerFactory.getLogger(AppDiscountController.class);

    @Autowired
    private PubDiscountService discountService;
    @Autowired
    private PubDeviceDiscountService deviceDiscountService;
    /**
     * 套餐列表
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listView", method = RequestMethod.POST)
    public Map<String,Object> listView(
            QueryPubDiscountCondition condition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            condition.setAdminId(adminId);
            List<PubDiscountDto> list = discountService.findDiscountList(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 添加套餐
     * @param discount
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String,Object> add(
            PubDiscount discount
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            discount.setAdminid(adminId);
//            discount.setDiscountname(discount.getMoney() + "元" + discount.getCoin() + "币");
            discountService.insert(discount);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 单个套餐
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editView", method = RequestMethod.POST)
    public Map<String,Object> editView(
            QueryPubDiscountCondition condition
    ){
        try {
            PubDiscountDto dto = discountService.findByCondition(condition);
            return WebResult.success().put("data",dto).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改套餐
     * @param discount
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Map<String,Object> edit(
            PubDiscount discount
    ){
        try {
//            discount.setDiscountname(discount.getMoney() + "元" + discount.getCoin() + "币");
            discountService.updateByPrimaryKeySelective(discount);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 批量更新套餐
     * @param command
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editBatch", method = RequestMethod.POST)
    public Map<String,Object> editBatch(
            UpdateBatchDiscountCommand command
    ){
        try {
            for (int i = 0; i < command.getDiscountid().length; i++) {
                PubDiscount discount = discountService.selectByPrimaryKey(command.getDiscountid()[i]);
//                discount.setDiscountname(command.getMoney()[i] + "元" + command.getCoin()[i] + "币");
                discount.setDiscountname(command.getDiscountName()[i]);
                discount.setMoney(command.getMoney()[i]);
                discount.setCoin(command.getCoin()[i]);
                discountService.updateByPrimaryKeySelective(discount);
            }
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 批量删除套餐
     * @param command
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Map<String,Object> deleteBatch(
            UpdateBatchDiscountCommand command,
            QueryPubDeviceDiscountCondition discountCondition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            discountCondition.setAdminId(adminId);
            for(Long discountId: command.getDiscountid()){
                discountCondition.setDiscountId(discountId);
                int flag = deviceDiscountService.checkDeviceDiscount(discountCondition);
                if(flag==0){
                    discountService.deleteByPrimaryKey(discountId);
                }else{
                    PubDiscount discount = discountService.selectByPrimaryKey(discountId);
                    return WebResult.failure("套餐:"+discount.getDiscountname()+"已经绑定设备,不能删除").toMap();
                }
            }
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备已绑定套餐界面
     * @param discountCondition
     * @param deviceDiscountCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDeviceDiscountView", method = RequestMethod.POST)
    public Map<String,Object> editDeviceDiscountView(
            QueryPubDiscountCondition discountCondition,
            QueryPubDeviceDiscountCondition deviceDiscountCondition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            discountCondition.setAdminId(adminId);
            deviceDiscountCondition.setAdminId(adminId);

            List<PubDiscountDto> discountList = discountService.findDiscountList(discountCondition);

            List<PubDiscountDto> discountDtoList = new ArrayList<PubDiscountDto>();

            for(PubDiscountDto discountDto : discountList){
                deviceDiscountCondition.setDiscountId(Long.valueOf(discountDto.getDiscountid()));
                Integer flag = deviceDiscountService.selectCheckDeviceDiscount(deviceDiscountCondition);
                if(flag==1){
                    discountDto.setFlag(1);
                }else{
                    discountDto.setFlag(0);
                }
                discountDtoList.add(discountDto);
            }

            return WebResult.success().put("data",discountDtoList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备添加修改套餐设置
     * @param discountIds 套餐id
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDeviceDiscount", method = RequestMethod.POST)
    public Map<String,Object> editDeviceDiscount(
            Long[] discountIds,
            QueryPubDeviceDiscountCondition condition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            condition.setAdminId(adminId);
            deviceDiscountService.deleteDeviceDiscount(condition);

            for (Long discountId : discountIds) {
                PubDeviceDiscount deviceDiscount = new PubDeviceDiscount();
                deviceDiscount.setDiscountid(discountId);
                deviceDiscount.setDeviceid(condition.getDeviceId());
                deviceDiscount.setAdminid(adminId);
                deviceDiscountService.insert(deviceDiscount);
            }

            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

}
